﻿using Microsoft.Extensions.ObjectPool;
using System;

namespace zijian666.Data
{
    internal class PooledObjectPolicy<T> : DefaultPooledObjectPolicy<T>
        where T : class, new()
    {
        private readonly Action<T> _returnning;

        public PooledObjectPolicy(Action<T> returnning = null) => _returnning = returnning;

        public override bool Return(T obj)
        {
            if (obj is IPooledObject pooled)
            {
                pooled.OnRetrunning();
            }
            _returnning?.Invoke(obj);
            return base.Return(obj);
        }
    }
}
